﻿using System;
using System.ComponentModel;
using System.Net;
using System.Windows;
namespace WPFExcelReport
{
    public partial class TAM04PLC
    {

        public TAM04PLC()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VAMSTAMM();
            this.DataContext = MV;
            InitializeComponent();
        }
        
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (IsLoaded)
                return;
            base.Window_Loaded(sender, e); 

        }
        public override void OnSave()
        {
            if (!IsSaved)
                return;
            dwData = TMV.VZL.TBMain.NewRow(); 
            dwData["DEVTYP"] = dwMaster["AMTYP"].ToString().Trim();
            dwData["MANDANT"] =app.Mandant;
            dwData["LOCALIP"] =dwMaster["AMHERST"].ToString().Trim();
            TMV.VZL.UZLAN02(dwData);
            TMV.UpdateAMSTAMM(dwMaster, 0);
            string key = KEY;
            KEY = "";
            KEY = key;
            base.OnSave();
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            { 
                AMTYP = "";
                AMART = "";
                AMKST = "";
                AMCD = "";
            }
            else
            {                
                AMTYP = TMV.AMTYP;
                AMART = TMV.AMART;
                AMKST = TMV.AMKSTKRS + AMKST;
                AMCD = TMV.AMVERFKZ;
                if (TMV.AMSTDVAR == "J")
                    IsSTD = true;
                else
                    IsSTD = false;
            }
        }
        public override void LOADBYAUF()
        {

            if (ORDERNUM.Length < 8)
                return;
            dwData = TMV.TBMain.NewRow();
            dwData["AMMANDANT"] = app.Mandant;
            dwData["AMNRKRS"] = "AND AMNRKRS= '" + ORDERNUM.Substring(0, 2) + "'";
            dwData["AMLFDNR"] = "AND AMLFDNR= '" + ORDERNUM.Substring(2, 4) + "'";
            dwData["AMPOS"] = "AND AMPOS= '" + ORDERNUM.Substring(6, 2) + "'";
            SResult = TMV.QAM03(dwData);
            MergeData(SResult, true);
            base.LOADBYAUF();
        }
        public override void OnLOChg(string lo)
        {
            if (lo.Length < 2)
                return;
            if (!IsEdit)
                return;
            TMV.AMLO = lo;
            base.OnLOChg(lo);
        }
        public override void OnLBChg(string lb)
        {
            if (lb.Length < 2)
                return;
            if (!IsEdit)
                return;
            TMV.AMLB = lb;
            base.OnLBChg(lb);
        }
        public override void OnSTDChg(bool value)
        {
            if (!IsEdit)
                return;
            if (IsSTD)
                TMV.AMSTDVAR = "J";
            else
                TMV.AMSTDVAR = "N";
            base.OnSTDChg(value);
        }
        public override void OnLPChg(string lp)
        {
            if (lp.Length < 8)
                return;
            if (!IsEdit)
                return;
            TMV.AMLPLATZ = lp;
            base.OnLPChg(lp);
        }
        public override void OnAMARTChg(string value)
        {
            if (value.Length < 8)
                return;
            if (!IsEdit)
                return;
            TMV.AMART = value;
            base.OnAMARTChg(value);
        }
        public override void OnAMTYPChg(string value)
        {
            if (value.Length < 8)
                return;
            if (!IsEdit)
                return;
          
            TMV.AMTYP = value;
            base.OnAMTYPChg(value);
        }
        public override void OnLIEFNRChg(string value)
        {
            if (!IsEdit)
                return;
            if (value.Length < 8)
                return;
            TMV.AMLIEFNR = value;
            base.OnLIEFNRChg(value);
        }
        public override void OnAMKSTChg(string value)
        {
            if (value.Length < 6)
                return;
            if (!IsEdit)
                return;
           
            TMV.AMKSTKRS = value.Substring(0,2);
            TMV.AMKST = value.Substring(2, 4);
            TMV.AMPKSTKRS = TMV.AMKSTKRS;
            TMV.AMPKST = TMV.AMKST;
            base.OnAMKSTChg(value);
        }
        public override void OnAMCDChg(string value)
        {
            if (value.Length < 1)
                return;
            if (!IsEdit)
                return;
            TMV.AMVERFKZ = AMCD;
            base.OnAMCDChg(value);
        }
        public override void OnEXAUFChg(string vlaue)
        {
            TBMain.Clear();
            TMV.CHKZLAM(EXAUF);
            vwMaster.View.MoveCurrentToFirst();
            base.OnEXAUFChg(vlaue);
        }
    }
}
